import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/pages/index.vue' //首页
import Login from  '@/pages/login/login.vue' //登录
import Examine from  '@/pages/onlinePractice/examine.vue'   //在线练习
import Song from  '@/pages/onlinePractice/column/song.vue'  //在线练习-栏目-歌曲练习
import Story from  '@/pages/onlinePractice/column/story.vue' //在线练习-栏目-故事练习 
import Word from  '@/pages/onlinePractice/column/word.vue'   //在线练习-栏目-单词练习
import LearningCenter from  '@/pages/learningCenter/learningCenter.vue' //学习中心
import PersonalCenter from  '@/pages/personalCenter/personalCenter.vue' //个人中心 
import UnitTest from  '@/pages/unitTest/unitTest.vue' //单元测试
import Live from  '@/pages/Live/live.vue' //直播

Vue.use(Router)
const routes = [
  {
    path: '/',
    name: 'home',
    meta:{
      requireAuth: true,
    },
    component: Home
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/onlinepractice/examine',
    component: Examine,
    children: [
        {
          path: '/column/song',
          component: Song
        },
        {
          path: '/column/story',
          component: Story
        },
        {
          path: '/column/word',
          component: Word
        }
    ]
  },
  {
    path: '/live',
    component: Live
  },
  {
    path: '/learningcenter',
    component: LearningCenter
  },
  {
    path: '/personalcenter',
    component: PersonalCenter
  },
  {
    path: '/unittest',
    component: UnitTest
  }
]
// 页面刷新时,重新赋值token
if(window.sessionStorage.getItem('token')) {
  console.log(111)
  window.sessionStorage.setItem('token',"token111")  // 保证页面刷新token仍有效
}

const router = new Router({
  routes
});

router.beforeEach((to, from, next) => {
  if (to.matched.some((r) => r.meta.requireAuth)) {
    if (window.sessionStorage.getItem('token')) {   //判断是否已经登录
      next();
    } else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}   //登录成功后重定向到当前页面
      });
    }
  } else {
    next();
  }
});
export default router;
